package com.cy.train.member.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.cy.train.common.context.LoginMemberContext;
import com.cy.train.common.resp.PageResp;
import com.cy.train.common.util.SnowUtil;
import com.cy.train.member.domain.Passenger;
import com.cy.train.member.domain.PassengerExample;
import com.cy.train.member.mapper.PassengerMapper;
import com.cy.train.member.req.PassengerQueryReq;
import com.cy.train.member.req.PassengerSaveReq;
import com.cy.train.member.resp.PassengerQueryResp;
import com.cy.train.member.service.PassengerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Date: 2024/2/27 14:48
 * @Author: cyzhang
 */
@Service
public class PassengerServiceImp implements PassengerService {
    private static final Logger LOG = LoggerFactory.getLogger(PassengerServiceImp.class);

    @Autowired
    private PassengerMapper passengerMapper;

    @Override
    public void save(PassengerSaveReq req) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);
        if(ObjectUtil.isNull(passenger.getId())) {
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        } else {
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKeySelective(passenger);
        }

    }

    @Override
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        Long memberId = req.getMemberId();
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("id desc");
        if(ObjectUtil.isNotNull(memberId)) {
            passengerExample.createCriteria().andMemberIdEqualTo(memberId);
        }

        LOG.info("当前页：{}", req.getPage());
        LOG.info("每页数量：{}", req.getSize());

        PageHelper.startPage(req.getPage(), req.getSize());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);

        PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);
        LOG.info("总条数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<PassengerQueryResp> passengerQueryRespList = BeanUtil.copyToList(passengerList, PassengerQueryResp.class);
        PageResp<PassengerQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(passengerQueryRespList);

        return pageResp;
    }

    @Override
    public void delete(Long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Passenger> queryAll() {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.createCriteria().andMemberIdEqualTo(LoginMemberContext.getId());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);
        return passengerList;
    }
}
